package com.securityLearn.sessionAuth.config.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 注入拦截器
 * @author duzq
 * @date 2021-8-26 21:41:01
 */
@Configurable
public class InterceptorConfig implements WebMvcConfigurer {
//    继承WebMvcConfigurer
//    因为是接口实现所以WebMvcAutoConfiguration还会默认配置，所以静态资源是可以访问的

    @Autowired
    private SimpleAuthenticationInterceptor simpleAuthenticationInterceptor;

    /**
     * 添加自定义拦截器
     */
    @Bean
    public InterceptorConfig getMyWebMvcConfig(){

        InterceptorConfig interceptorConfig = new InterceptorConfig() {
            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                // 设置需要拦截的目录
                registry.addInterceptor(simpleAuthenticationInterceptor).addPathPatterns("/r/**");
            }
        };
        return interceptorConfig;
    }
}


//@Configurable
//public class InterceptorConfig extends WebMvcConfigurationSupport {
//    // 继承WebMvcConfigurationSupport
//    // 会导致WebMvcAutoConfiguration不加载，所以默认配置丢失导致静态资源等无法访问
//
//    @Autowired
//    private SimpleAuthenticationInterceptor simpleAuthenticationInterceptor;
//
//    /**
//     * 添加自定义拦截器
//     * @param registry
//     */
//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(simpleAuthenticationInterceptor).addPathPatterns("/r/**");
//        super.addInterceptors(registry);
//    }
//
//    /**
//     * 添加此方法恢复静态资源访问
//     * @param registry
//     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:/resources/")
//                .addResourceLocations("classpath:/static/")
//                .addResourceLocations("classpath:/public/");
//    }
//}
////    WebMvcConfigurationSupport 与 WebMvcConfigurer 的区别：
////        https://blog.csdn.net/weixin_43153309/article/details/88639130
////        ?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%
////        7EBlogCommendFromMachineLearnPai2%7Edefault-1.essearch_pc_relevant
////        &depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%
////        7EBlogCommendFromMachineLearnPai2%7Edefault-1.essearch_pc_relevant
